import { Ast, MultiVisitor, Visitor, createRootAst } from "./ast";
import { scanDialogOnce } from "./scanDialog";
import { tryRun } from "./utils";

export interface AdvContext {
    close?: () => void;
    name?: string;
    node?: Ast;
}

export class AdvVisitor implements Visitor<AdvContext>{
    visitNode(node: Ast, ctx: AdvContext): void {
        if (ctx.close) return;
        if (!node.visibleToUser) return;
        if (node.className) {
            const classNameSplit = node.className.split('.')
            const classNameEnd = classNameSplit.pop()
            const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
            if (this[methodName]) return this[methodName](node, ctx)
        }
        for (let c of node.children) {
            this.visitNode(c, ctx)
        }
    }
    getMethod(name: string) {
        const classNameSplit = name.split('.')
        const classNameEnd = classNameSplit.pop()
        const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
        if (this[methodName]) return this[methodName].bind(this)
    }
}

/**
 * 

<android.widget.FrameLayout size="1080 2268" position="0 0 1080 2268" visible="true">
    <android.widget.TextView inScreen="true" size="441 89" position="320 229 761 318" text="恭喜您获得优惠" visible="true">
    </android.widget.TextView>
    <android.widget.TextView inScreen="true" size="630 47" position="225 328 855 375" text="您可以点击小房子，页面右上角“优惠券”查看" visible="true">
    </android.widget.TextView>
    <android.widget.TextView inScreen="true" size="39 120" position="352 858 391 978" text="¥" visible="true">
    </android.widget.TextView>
    <android.widget.TextView inScreen="true" size="336 168" position="391 820 727 988" text="3666" visible="true">
    </android.widget.TextView>
    <android.widget.TextView inScreen="true" size="264 58" position="408 1009 672 1067" text="返最高3666元" visible="true">
    </android.widget.TextView>
    <android.widget.TextView inScreen="true" size="424 45" position="328 1216 752 1261" text="使用时间 2024.3.6~2024.5.31" visible="true">
    </android.widget.TextView>
    <android.view.ViewGroup inScreen="true" size="668 188" position="206 1456 874 1644" clickable="true" visible="true">
    </android.view.ViewGroup>
    <android.view.ViewGroup inScreen="true" size="566 120" position="257 1524 823 1644" clickable="true" visible="true">
        <android.widget.TextView inScreen="true" size="566 96" position="257 1536 823 1632" text="您可以点击小房子，页面右上角“优惠券”查看" visible="true">
        </android.widget.TextView>
    </android.view.ViewGroup>
    <android.widget.TextView inScreen="true" size="270 39" position="253 1701 523 1740" text="领取表示已阅读并同意" visible="true">
    </android.widget.TextView>
    <android.view.ViewGroup inScreen="true" size="304 39" position="523 1701 827 1740" clickable="true" visible="true">
        <android.widget.TextView inScreen="true" size="304 39" position="523 1701 827 1740" text=" 个人信息授权与保护声明" visible="true">
        </android.widget.TextView>
    </android.view.ViewGroup>
</android.widget.FrameLayout>
 */


export class Adv10Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `恭喜您获得优惠`) {
            const clickNode = node.parent.findOne(it => it.className === `android.view.ViewGroup` && it.children.length === 0 && it.clickable)
            if (clickNode) {
                ctx.close = () => clickNode.click()
                return;
            }
        }
    }
}


export class Adv9Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `免打扰升级` && node.sid === 'tv_upgrade_now') {
            ctx.close = () => node.click()
            return;
        }
    }
}

export class Adv8Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `暂不生成`) {
            const item = node.findInParent((ast) => ast.clickable && ast.className === `android.view.ViewGroup`)
            if (item) {
                ctx.close = () => item.click()
                return;
            }
        }
    }
}


export class Adv7Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `下一步` && node.parent?.clickable) {
            ctx.close = () => node.click()
            return;
        }
    }
}

export class Adv6Visitor extends AdvVisitor {
    visitButton(node: Ast, ctx: AdvContext) {
        if (node.text === `看视频 领金币`) {
            let closeBtn: Ast;
            node.parent.parent.parent.children.map(it => {
                if (it.className === `android.view.ViewGroup`) {
                    closeBtn = it.children.find(it => it.className === `android.widget.ImageView` && it.clickable)
                }
            })
            if (closeBtn) {
                ctx.close = () => closeBtn.click()
                return;
            }
        }
    }
}
export class Adv5Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `收到快手的提现红包` || node.text === `获得一笔现金打款`) {
            const closeBtn = node.findOneInParent(it => it.className === `android.widget.ImageView` && it.clickable && it.sid.length === 0)
            if (closeBtn) {
                ctx.close = () => closeBtn.click()
                return;
            }
        }
    }
}

export class Adv4Visitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `账号激活`) {
            ctx.close = () => {
                events.broadcast.emit('ERROR', node.text || '')
            }
            return;
        }
        if (node.text === `请完成安全验证`) {
            ctx.close = () => {
                events.broadcast.emit('ERROR', node.text || '')
            }
            return;
        }
    }
}

export class Adv3Visitor extends AdvVisitor {
    node: Ast;
    has: boolean;
    visitNode(node: Ast, ctx: AdvContext): void {
        super.visitNode(node, ctx)
        if (this.has && this.node) {
            const node = this.node;
            ctx.close = () => node.click()
        }
    }
    visitImageView(node: Ast, ctx: AdvContext) {
        if (node.sid === 'close') {
            this.node = node;
        }
    }
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === '我要吐槽') {
            this.has = true;
        }
    }
}

export class Adv2Visitor extends AdvVisitor {
    visitView(node: Ast, ctx: AdvContext) {
        if (node.sid === `pymi_user_label`) {
            return;
        }
        if (node.sid === `avatar_right_bottom_username_view`) {
            return;
        }
        if (node.sid === `container`) {
            return;
        }
        if (node.parent?.sid === 'nasa_groot_view_pager') {
            return;
        }
        if (node.clickable && node.text === '我知道了') {
            ctx.close = () => node.click()
            return;
        }
    }
}

export class Adv1Visitor extends AdvVisitor {
    visitButton(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith('button1') && node.text === '允许') {
            ctx.close = () => node.click()
            return;
        }
    }
}

export class FangZiAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `您想买哪种户型的房子？` || node.text === `为帮你找到更优惠的房子，我想知道`) {
            const closeable = node.parent.children[0]
            if (closeable.className === `android.view.ViewGroup`) {
                ctx.close = () => closeable.click()
                return;
            }
        }
    }
}

export class QuTianXie2AdvVisitor extends AdvVisitor {
    quTianXie: boolean;
    hasTitle: boolean;
    visitNode(node: Ast, ctx: AdvContext): void {
        super.visitNode(node, ctx)
        if (this.quTianXie && this.hasTitle) {
            if (ctx.node) {
                const closeable = ctx.node;
                ctx.close = () => closeable.click()
            }
        }
    }
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `去填写`) {
            this.quTianXie = true;
        }
        if (node.text === `找工作上快手`) {
            this.hasTitle = true;
        }
    }
    visitViewGroup(node: Ast, ctx: AdvContext) {
        if (node.children.length === 0) {
            ctx.node = node;
        } else {
            node.children.map(c => c.visit(this, ctx))
        }
    }
}

export class FriendRecAdvVisitor extends AdvVisitor {
    has: boolean = false;
    node: Ast;
    visitNode(node: Ast, ctx: AdvContext): void {
        super.visitNode(node, ctx)
        if (this.has && this.node) {
            const n = this.node;
            ctx.close = () => n.click()
            ctx.name = `FriendRecAdvVisitor`;
            return;
        }
    }
    visitImageView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`close_btn`) && node.clickable) {
            this.node = node;
        }
    }
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `朋友推荐` && node.id.endsWith(`title_view`)) {
            this.has = true;
        }
    }
}

export class KemTaskPendantNewVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`pendant_close_area`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `KemTaskPendantNewVisitor`
        }
    }
}

export class NasaGrootViewPagerAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`splash_skip_text`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `NasaGrootViewPagerAdvVisitor`
        }
    }
}


export class ZhiDaoLeAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `知道了` && !node.clickable) {
            if (node.parent.clickable && node.parent.className === `android.view.ViewGroup` && node.parent.parent.className === `android.widget.FrameLayout`) {
                ctx.close = () => node.parent.click()
                ctx.name = `ZhiDaoLeAdvVisitor`
            }
        }

    }
}


export class LiJiLingQuAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `立即领取` && node.visibleToUser) {
            if (node.parent) {
                const n = selector().clickable(true).className('android.view.ViewGroup').findOne(1000 * 5)
                if (n) {
                    ctx.close = () => n.click()
                    ctx.name = `LiJiLingQuAdvVisitor`
                }
            } else {
                // 领取新人红包
                const n = selector().clickable(true).className('android.widget.ImageView').findOne(1000 * 5)
                if (n) {
                    ctx.close = () => n.click()
                    ctx.name = `LiJiLingQuAdvVisitor`
                }
            }
        }
        if (node.id.endsWith(`set_teenage_mode`) && node.text === `开启青少年模式`) {
            node.parent.parent.visit(new INowVisitor(), ctx)
            return;
        }

    }
}

export class INowVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.sid === `pymi_user_label`) {
            return;
        }
        if (node.sid === `avatar_right_bottom_username_view`) {
            return;
        }
        if (node.sid === `container`) {
            return;
        }
        if (node.parent?.sid === 'nasa_groot_view_pager') {
            return;
        }
        if (node.text === `我知道了` && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `INowVisitor`
        }
        if (node.text === '我知道了' && node.parent.clickable) {
            ctx.close = () => node.parent.click()
            ctx.name = `INowVisitor`
        }
    }
}

export class CaiNiXiHuanAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `退出直播间`) {
            if (node.parent && node.parent.clickable) {
                ctx.close = () => node.parent?.click()
                ctx.name = `CaiNiXiHuanAdvVisitor`
                return;
            }
        }
        if (node.text === `退出`) {
            if (node.parent && node.parent.clickable) {
                ctx.close = () => node.parent?.click()
                ctx.name = `CaiNiXiHuanAdvVisitor`
                return;
            }
        }
    }
}


export class LiJiGuanZhuAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (/立即关注.*/.test(node.text)) {
            if (node.parent && node.parent.clickable) {
                if (node.parent.parent) {
                    const n = node.parent.parent.children.find(it => it !== node.parent && it.className === `android.view.ViewGroup` && it.clickable)
                    if (n) {
                        ctx.close = () => n.click()
                        ctx.name = `LiJiGuanZhuAdvVisitor`
                    } else {
                        ctx.close = () => back();
                        ctx.name = `LiJiGuanZhuAdvVisitor`
                    }
                }
            }
        }
    }
}

export class MoRenJingYinAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `取消` && node.id.endsWith(`negative`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `MoRenJingYinAdvVisitor`
            return;
        }
        if (node.text === `领取并使用` && node.visibleToUser) {
            ctx.close = () => scanDialogOnce()
            ctx.name = `LingQuBingShiYongAdvVisitor`
            return;
        }
        if (node.text === `关注并领取` && node.visibleToUser) {
            ctx.close = () => scanDialogOnce()
            ctx.name = `GuanZhuBingLingQuAdvVisitor`
            return;
        }
        if (node.text === `恭喜获得免费红包`) {
            if (node.parent && node.parent.children) {
                const n = node.parent.children.find(it => it !== node && it.className === `android.widget.ImageView` && it.clickable)
                if (n) {
                    ctx.close = () => n.click()
                    ctx.name = `RedPacketAdvVisitor`
                }
            }
        }
    }
}

export class GameAdvVisitor extends AdvVisitor {
    visitView(node: Ast, ctx: AdvContext): void {
        if (node.id.endsWith(`teamBlue`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `GameAdvVisitor`
        }
    }
}

export class KanShiPinLingQuAdvVisitor extends AdvVisitor {
    visitButton(node: Ast, ctx: AdvContext) {
        if (node.text === `看视频领取` && node.clickable) {
            ctx.close = () => back()
        }
    }
}


export class XinRenFuliAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext): void {
        if (node.text === `立即领取` && !node.clickable && node.parent.className === `android.view.ViewGroup`) {
            const item = node.parent?.parent?.parent?.children?.find(it => it.className === `android.widget.ImageView` && it.clickable)
            if (item) {
                ctx.close = () => item.click()
                ctx.name = `XinRenFuliAdvVisitor`
            } else {
                ctx.close = () => back()
                ctx.name = `XinRenFuliAdvVisitor`
            }
        }
    }
}

export class TaskFailAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`empty_desc_text_view`) && node.text === `很遗憾，本次任务失败`) {
            ctx.close = () => back()
            ctx.name = `TaskFailAdvVisitor`
        }
    }
}

export class QuKanKanAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `去看看` && node.parent.className === `android.view.ViewGroup` && node.parent?.parent.className === `android.view.ViewGroup`) {
            const item = node.parent?.parent?.children?.find(it => it.className === `android.view.ViewGroup` && it.clickable && it !== node.parent)
            if (item) {
                // 存在误点的情况
                ctx.close = () => item.click()
                ctx.name = `QuKanKanAdvVisitor`
                return;
            }
        }
        if (node.text === `去看看` && node.parent.className === `android.view.ViewGroup` && node.parent?.parent.className === `android.widget.FrameLayout`) {
            const item = node.parent?.parent?.children?.find(it => it.className === `android.view.ViewGroup` && it.clickable && it !== node.parent)
            if (item) {
                // 存在误点的情况
                ctx.close = () => item.click()
                ctx.name = `QuKanKanAdvVisitor`
                return;
            }
        }
    }
}

export class KsTiXianHongBaoAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `收到快手的提现红包`) {
            const item = node.parent.parent.children.find(it => it.className === `android.widget.ImageView` && it.clickable)
            if (item) {
                ctx.close = () => item.click()
                ctx.name = `KsTiXianHongBaoAdvVisitor`
            } else {
                ctx.close = () => back()
                ctx.name = `KsTiXianHongBaoAdvVisitor`
            }
            ctx.name = `KsTiXianHongBaoAdvVisitor`
        }
    }
}


export class ShangPinAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `商品页面`) {
            ctx.close = () => back()
            ctx.name = `ShangPinAdvVisitor`
        }
    }
}

export class QuTianXieAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `下次再说`) {
            ctx.close = () => node.parent.click()
            ctx.name = `QuTianXieAdvVisitor`
        }
    }
}

export class XianShiFuLiAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `限时福利，试试手气`) {
            const children = node.parent.children.filter(it => it.clickable)
            if (children.length > 0) {
                ctx.close = () => children[children.length - 1].click()
            }
            ctx.name = `XianShiFuLiAdvVisitor`
        }
    }
}

export class GiveUpAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `放弃机会` && node.parent.clickable) {
            ctx.close = () => node.parent.click()
            ctx.name = `GiveUpAdvVisitor`
        }
    }
}


export class RootFrameLayoutVisitor extends AdvVisitor {
    visitNode(node: Ast, ctx: AdvContext): void {
        if (node.className === `android.widget.FrameLayout`) {
            if (node.children.length === 1) {
                const n = node.children[0];
                if (n.className === 'android.view.ViewGroup' && n.clickable) {
                    if (n.children.length === 1) {
                        // ctx.close = () => back()
                        // ctx.name = `RootFrameLayoutVisitor`
                    }
                }
            }
        }
        return;
    }
}

export class LingHongBaoVisitor extends AdvVisitor {
    visitImageView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`dialog_close`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `LingHongBaoVisitor`
        }
    }
}

export class ZhiboJieShuVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`live_play_closed_v3_title_text_view`) && node.text === `直播已结束`) {
            // ctx.close = () => {
            //     back()
            // }
            return;
        }
    }
}


export class RefundAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`btn_game_interactive_auth_dlg_deny`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `RefundAdvVisitor`
        }
    }
}


export class WangZheZuQiuAdvVisitor extends AdvVisitor {
    visitFrameLayout(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`game_interactive_panel_view`)) {
            const root = createRootAst()
            new PlayViewClickVisitor().visitNode(root, ctx)
            return;
        }
        node.children.map(c => c.visit(this, ctx))
    }
}


export class OpenKwaiShouVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext): void {
        if (node.id.endsWith('event_title') && node.text.includes(`老哥助手`)) {
            const btn = node.parent.children.find(it => it.text === `允许`)
            if (btn && btn.clickable) {
                ctx.close = () => btn.click()
                ctx.name = `OpenKwaiShouVisitor`
            }
        }
    }
}

export class AdvMultiAdvVisitor extends MultiVisitor<AdvContext> {
    visitors: AdvVisitor[] = []
    constructor(visitors: AdvVisitor[]) {
        super(visitors);
    }
    visitNode(node: Ast, ctx: AdvContext): void {
        if (ctx.close) return;
        const methods = this.visitors.map(it => it.getMethod(node.className)).filter(it => !!it)
        for (let method of methods) {
            if (ctx.close) return;
            method(node, ctx)
        }
    }
}


export class ClickRetryVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`retry_btn`) && node.clickable && node.text === `点击重试`) {
            ctx.close = () => node.click()
            ctx.name = `ClickRetryVisitor`
        }
    }
}


export class PlayViewClickVisitor extends AdvVisitor {
    visitFrameLayout(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`play_view_wrapper`) && node.clickable) {
            ctx.close = () => node.clickRect()
            ctx.name = `PlayViewClickVisitor`
            return;
        }
        node.children.map(c => c.visit(this, ctx))
    }
}


export class ForwardPanelItemCancelVisitor extends AdvVisitor {
    visitImageView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`forward_panel_item_cancel`) && node.clickable) {
            if (ctx.name === `ForwardPanelItemCancelVisitor`) {
                ctx.close = () => node.click()
            }
        }
    }
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`textTitle`) && node.text === `分享到`) {
            ctx.name = `ForwardPanelItemCancelVisitor`;
        }
    }
}

export class PhotoFeedSideBarCloseViewAdvVisitor extends AdvVisitor {
    visitRecyclerView(node: Ast, ctx: AdvContext) {
        if (node.id.endsWith(`photo_feed_side_bar_close_view`) && node.clickable) {
            ctx.close = () => node.click()
            ctx.name = `PhotoFeedSideBarCloseViewAdvVisitor`
        }
    }
}

export class HuLueAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext): void {
        if (node.text === `忽略` && node.parent.className === `android.view.ViewGroup` && node.parent.clickable) {
            ctx.close = () => node.parent.click()
            ctx.name = `HuLueAdvVisitor`
            return;
        }
        if (node.text === `专属红包` || node.text === '新人福利，试试手气') {
            ctx.close = () => {
                const clickable = node.parent?.parent?.nextSibling()
                if (clickable && clickable.clickable) clickable.click()
                else scanDialogOnce()
            }
            return;
        }
        if (node.text.includes(`红包溜走`)) {
            ctx.close = () => scanDialogOnce()
            return;
        }
    }
}


export class ShengRiVisitor extends AdvVisitor {
    visitNode(node: Ast, ctx: AdvContext): void {
        if (node.className === `android.widget.FrameLayout`) {
            if (node.children.length === 1) {
                const n = node.children[0];
                if (n && n.id.endsWith(`live_activity_popup_root_view`) && n.clickable) {
                    ctx.close = () => n.click()
                    ctx.name = `ShengRiVisitor`
                }
            }
        }
    }
}

export class YiJianFangFeiAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext): void {
        if (node.text === `许愿并关注`) {
            const parent = node.parent;
            const children = parent?.parent?.children;
            if (children && children.length > 0) {
                const child = children.find(it => it.className === `android.view.ViewGroup` && it.clickable)
                if (child) {
                    ctx.close = () => child.click()
                    ctx.name = `YiJianFangFeiAdvVisitor`
                }
            }
        }
    }
}

export class JobAdvVisitor extends AdvVisitor {
    visitTextView(node: Ast, ctx: AdvContext) {
        if (node.text === `立刻投递，求职先人一步～`) {
            ctx.close = () => back()
        }
        if (node.text === `勋章功能上线了`) {
            ctx.close = () => back()
        }
    }
}
export const advVisitors: AdvVisitor[] = [
    new FangZiAdvVisitor(),
    new FriendRecAdvVisitor(),
    new KemTaskPendantNewVisitor(),
    new NasaGrootViewPagerAdvVisitor(),
    new ZhiDaoLeAdvVisitor(),
    new LiJiLingQuAdvVisitor(),
    new CaiNiXiHuanAdvVisitor(),
    new MoRenJingYinAdvVisitor(),
    new GameAdvVisitor(),
    new XinRenFuliAdvVisitor(),
    new QuKanKanAdvVisitor(),
    new KanShiPinLingQuAdvVisitor(),
    new KsTiXianHongBaoAdvVisitor(),
    new ShangPinAdvVisitor(),
    new TaskFailAdvVisitor(),
    new QuTianXieAdvVisitor(),
    new QuTianXie2AdvVisitor(),
    new XianShiFuLiAdvVisitor(),
    new GiveUpAdvVisitor(),
    new RootFrameLayoutVisitor(),
    new LingHongBaoVisitor(),
    new RefundAdvVisitor(),
    new WangZheZuQiuAdvVisitor(),
    new ZhiboJieShuVisitor(),
    new OpenKwaiShouVisitor(),
    new ClickRetryVisitor(),
    new HuLueAdvVisitor(),
    new ForwardPanelItemCancelVisitor(),
    new PhotoFeedSideBarCloseViewAdvVisitor(),
    new ShengRiVisitor(),
    new YiJianFangFeiAdvVisitor(),
    new JobAdvVisitor(),
    new Adv1Visitor(),
    new Adv2Visitor(),
    new Adv3Visitor(),
    new Adv4Visitor(),
    new Adv5Visitor(),
    new Adv6Visitor(),
    new Adv7Visitor(),
    new Adv8Visitor(),
    new Adv10Visitor(),
    new Adv9Visitor(),
]
export function createAdvVisitor(visitors: AdvVisitor[]) {
    return new AdvMultiAdvVisitor([
        ...advVisitors,
        ...visitors
    ]);
}
export function clearAdv(visitors: AdvVisitor[]) {
    const visitor = new AdvMultiAdvVisitor([
        new FangZiAdvVisitor(),
        new FriendRecAdvVisitor(),
        new KemTaskPendantNewVisitor(),
        new NasaGrootViewPagerAdvVisitor(),
        new ZhiDaoLeAdvVisitor(),
        new LiJiLingQuAdvVisitor(),
        new CaiNiXiHuanAdvVisitor(),
        new MoRenJingYinAdvVisitor(),
        new GameAdvVisitor(),
        new XinRenFuliAdvVisitor(),
        new QuKanKanAdvVisitor(),
        new KanShiPinLingQuAdvVisitor(),
        new KsTiXianHongBaoAdvVisitor(),
        new ShangPinAdvVisitor(),
        new TaskFailAdvVisitor(),
        new QuTianXieAdvVisitor(),
        new QuTianXie2AdvVisitor(),
        new XianShiFuLiAdvVisitor(),
        new GiveUpAdvVisitor(),
        new RootFrameLayoutVisitor(),
        new LingHongBaoVisitor(),
        new RefundAdvVisitor(),
        new WangZheZuQiuAdvVisitor(),
        new ZhiboJieShuVisitor(),
        new OpenKwaiShouVisitor(),
        new ClickRetryVisitor(),
        new HuLueAdvVisitor(),
        new ForwardPanelItemCancelVisitor(),
        new PhotoFeedSideBarCloseViewAdvVisitor(),
        new ShengRiVisitor(),
        new YiJianFangFeiAdvVisitor(),
        new JobAdvVisitor(),
        new Adv1Visitor(),
        new Adv2Visitor(),
        new Adv3Visitor(),
        new Adv4Visitor(),
        new Adv5Visitor(),
        new Adv6Visitor(),
        new Adv7Visitor(),
        new Adv8Visitor(),
        new Adv10Visitor(),
        new Adv9Visitor(),
        ...visitors
    ]);
    visitor.setStop((ctx: AdvContext) => {
        if (ctx.close) return true;
        return false;
    })
    const ctx: AdvContext = {}
    const root = createRootAst()
    visitor.visitNode(root, ctx)
    if (ctx.close) {
        tryRun(() => {
            ctx.close()
            ctx.close = undefined;
        });
    }
}
/**
 * 每秒检测广告 并关闭他们
 */
export function watchAdv(visitors: AdvVisitor[]) {
    while (true) {
        tryRun(() => clearAdv(visitors));
        sleep(1000)
    }
}
